package com.example.extra.service;

import com.example.extra.common.CustomUserDetails;
import com.example.extra.common.JwtProperties;
import com.example.extra.response.JwtResponse;

import com.example.extra.utils.JwtUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Collection;


/**
 * @author lixiang
 */
@Service
public class LoginService {


    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private JwtProperties jwtProperties;

    public JwtResponse login(Authentication authentication){
        SecurityContextHolder.getContext().setAuthentication(authentication);
        CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
        String jwt = jwtUtils.generateAccessToken(userDetails);
        return new JwtResponse(
                jwt,
                jwtProperties.getExpiration()
        );
    }
}
